"""FPGA runtime library for TAPA."""

# Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors.
# All rights reserved. The contributor(s) of this file has/have agreed to the
# RapidStream Contributor License Agreement.

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_test")
load("@rules_pkg//pkg:mappings.bzl", "pkg_filegroup", "pkg_files", "strip_prefix")
load("//bazel:dpi_rules.bzl", "dpi_legacy_rdi_library", "dpi_library")

cc_library(
    name = "frt",
    srcs = glob(
        [
            "src/**/*.cpp",
            "src/**/*.h",
        ],
        exclude = [
            "src/frt/**/*_test.cpp",
            "src/frt/devices/tapa_fast_cosim_dpi.cpp",
        ],
    ),
    hdrs = [
        "src/frt.h",
        "src/frt/arg_info.h",
        "src/frt/buffer.h",
        "src/frt/buffer_arg.h",
        "src/frt/device.h",
        "src/frt/devices/shared_memory_queue.h",
        "src/frt/devices/shared_memory_stream.h",
        "src/frt/stream.h",
        "src/frt/stream_arg.h",
        "src/frt/stringify.h",
        "src/frt/tag.h",
    ],
    copts = [
        "-DCL_HPP_CL_1_2_DEFAULT_BUILD",
        "-DCL_HPP_TARGET_OPENCL_VERSION=120",
        "-DCL_HPP_MINIMUM_OPENCL_VERSION=120",
        "-DCL_TARGET_OPENCL_VERSION=120",
    ],
    includes = ["src"],
    linkopts = [
        "-lstdc++fs",
        "-lrt",
    ],
    visibility = ["//visibility:public"],
    deps = [
        "@boost//:endian",
        "@glog",
        "@nlohmann_json//:json",
        "@tinyxml2",
        "@xilinx_xrt//:xrt",
    ],
)

cc_test(
    name = "frt-test",
    size = "small",
    srcs = glob(["**/*_test.cpp"]),
    deps = [
        ":frt",
        "@googletest//:gtest",
        "@googletest//:gtest_main",
    ],
)

dpi_library(
    name = "tapa_fast_cosim_dpi_xv",
    srcs = [
        "src/frt/devices/shared_memory_queue.cpp",
        "src/frt/devices/tapa_fast_cosim_dpi.cpp",
    ],
    hdrs = [
        "src/frt/devices/shared_memory_queue.h",
    ],
    includes = ["src"],
    visibility = ["//tapa/cosim:__pkg__"],
    deps = [
        "@glog",
        "@xsim_xv//:svdpi",
    ],
)

dpi_legacy_rdi_library(
    name = "tapa_fast_cosim_dpi_legacy_rdi",
    srcs = [
        "src/frt/devices/shared_memory_queue.cpp",
        "src/frt/devices/tapa_fast_cosim_dpi.cpp",
    ],
    hdrs = [
        "src/frt/devices/shared_memory_queue.h",
    ],
    includes = ["src"],
    visibility = ["//tapa/cosim:__pkg__"],
    deps = [
        "@glog",
        "@xsim_legacy_rdi//:svdpi",
    ],
)

filegroup(
    name = "include",
    srcs = [
        "src/frt.h",
        "src/frt/arg_info.h",
        "src/frt/buffer.h",
        "src/frt/buffer_arg.h",
        "src/frt/device.h",
        "src/frt/devices/shared_memory_queue.h",
        "src/frt/devices/shared_memory_stream.h",
        "src/frt/stream.h",
        "src/frt/stream_arg.h",
        "src/frt/stringify.h",
        "src/frt/tag.h",
    ],
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "pkg-lib",
    srcs = [
        ":frt",
        ":tapa_fast_cosim_dpi_legacy_rdi",
        ":tapa_fast_cosim_dpi_xv",
    ],
    prefix = "usr/lib",
    strip_prefix = strip_prefix.files_only(),
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "pkg-include",
    srcs = [":include"],
    prefix = "usr/include",
    strip_prefix = strip_prefix.from_pkg("src"),
)

pkg_files(
    name = "pkg-extra-runtime-deps",
    srcs = [
        "@gflags",
        "@glog",
        "@tinyxml2",
    ],
    prefix = "usr/lib",
    strip_prefix = strip_prefix.files_only(),
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "pkg",
    srcs = [
        ":pkg-include",
        ":pkg-lib",
    ],
    visibility = ["//visibility:public"],
)

pkg_filegroup(
    name = "pkg-portable",
    srcs = [
        ":pkg",
        ":pkg-extra-runtime-deps",
    ],
    visibility = ["//visibility:public"],
)
